﻿using System;
using System.Windows;
using System.Windows.Controls;
using FluorineFx;
using FluorineFx.AMF3;
using FluorineFx.Messaging.Api.Service;
using FluorineFx.Net;
using System.Net;
using System.IO;

namespace SliverlightApp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnFluorineFx_Click(object sender, RoutedEventArgs e)
        {
            NetConnection _netConnection = new NetConnection();
            _netConnection.ObjectEncoding = ObjectEncoding.AMF3;
            _netConnection.NetStatus += new NetStatusHandler(_netConnection_NetStatus);
            _netConnection.Connect("http://localhost:1718/Gateway.aspx");
            _netConnection.Call("ServiceLib.TestLib.GetPersonList", new GetPersonHandler(this));

        }

        private void _netConnection_NetStatus(object sender, NetStatusEventArgs e)
        {
            string level = e.Info["level"] as string;
            this.Dispatcher.BeginInvoke(() => { this.txtResult.Text = "level:" + level + "，code：" + e.Info["code"] as String; });

        }

        private class GetPersonHandler : IPendingServiceCallback
        {
            MainPage page;

            public GetPersonHandler(MainPage page)
            {
                this.page = page;
            }

            public void ResultReceived(IPendingServiceCall call)
            {
                page.Dispatcher.BeginInvoke(() => { page.txtResult.Text = ""; });
                object result = call.Result;
                ArrayCollection items = result as ArrayCollection;
                foreach (object item in items)
                {
                    Person p = item as Person;
                    page.Dispatcher.BeginInvoke(() => { page.txtResult.Text += p.ToString() + "；"; });                    
                }

            }
        }

        /// <summary>
        /// wcf测试(List方法)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWcf_Click(object sender, RoutedEventArgs e)
        {
            WebClient client = new WebClient();
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            client.OpenReadAsync(new Uri("http://localhost:1718/test.svc/GetPersonList"));
        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    StreamReader _Reader = new StreamReader(e.Result);
                    txtResult.Text = _Reader.ReadToEnd();
                    _Reader.Close();
                });
            }
            else 
            {
                this.Dispatcher.BeginInvoke(() => { txtResult.Text = "错误：" + e.Error.Message.ToString(); });
            }
        }

        /// <summary>
        /// wcf测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWcf2_Click(object sender, RoutedEventArgs e)
        {
            WebClient client = new WebClient();
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted2);
            client.OpenReadAsync(new Uri("http://localhost:1718/test.svc/GetPersonList2"));
        }

        void client_OpenReadCompleted2(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    StreamReader _Reader = new StreamReader(e.Result);
                    txtResult.Text = _Reader.ReadToEnd();
                    _Reader.Close();
                });
            }
            else
            {
                this.Dispatcher.BeginInvoke(() => { txtResult.Text = "错误：" + e.Error.Message.ToString(); });
            }
        }

        
    }
}
